package cn.youyeku.zsxh.ui.activity.tabAct;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.getbase.floatingactionbutton.FloatingActionsMenu;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import cn.youyeku.zsxh.R;
import cn.youyeku.zsxh.Utils.LogUtils;
import cn.youyeku.zsxh.adapter.RefreshFootAdapter;
import cn.youyeku.zsxh.api.XhApi;
import cn.youyeku.zsxh.bean.Question_center;
import cn.youyeku.zsxh.ui.UIControl;
import cn.youyeku.zsxh.ui.activity.ABS.AbsSwipeActivity;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by youye on 17-4-4.
 */

public class ActGoodsExchangeAccepted extends AbsSwipeActivity {

FloatingActionsMenu fgm_good_exchange_shop_btn_fabMenu;
    public ActGoodsExchangeAccepted() {
        super( R.id.fgm_base_recycle, R.id.fgm_base_swipe);

    }

    @Override
    protected RefreshFootAdapter getAapter() {
        return new RefreshFootAdapter<Question_center>(R.layout.fgm_goods_exchange_item,R.layout.refresh_footer) {

            @Override
            protected void onBindCustomerData(RefreshViewHolder holder, final Question_center data, int pos) {
                ImageView imgHead = holder.getChildView(R.id.good_exchange_imageV_head);
                TextView nick_name = holder.getChildView(R.id.good_exchange_textV_nickname);
                TextView date = holder.getChildView(R.id.good_exchange_textV_date);
                TextView detail = holder.getChildView(R.id.good_exchange_textV_detail_text);
                TextView price = holder.getChildView(R.id.good_exchange_textV_price);
                TextView btn_py = holder.getChildView(R.id.good_exchange_textV_btn_py);
                ImageView pic = holder.getChildView(R.id.good_exchange_imgV_pic);


                price.setText("标题："+data.getShop_title());
                date.setText("时间：" +data.getShop_goods_time());
                detail.setText("问题描述：" +data.getShop_content());
                nick_name.setText(data.getNick_name());

                Glide.with(getApplicationContext())
                        .load(XhApi.getFace(data.getShop_author_id()))
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .error(R.drawable.default_avatar)
                        .into(imgHead);
                Glide.with(getApplicationContext())
                        .load(XhApi.getShop(data.getShop_author_id(),data.getShop_pic()))
                        .diskCacheStrategy(DiskCacheStrategy.NONE)

                        .into(pic);

                imgHead.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UIControl.showUserInfo(getApplicationContext(), data.getShop_author_id());
                    }
                });
//                if(data.getShop_is_sale().equals("1")){
////                    btn_py.setText("交易中");
//                }

            }

            @Override
            protected void setmOnItemClick(View itemView, final Question_center data, int pos) {
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UIControl.showActGoodsExchange(getApplicationContext(),data);

                    }
                });
            }
        };
    }

    @Override
    protected void requestData() {

        XhApi.get_shop_accepted(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String data = response.body().string();

                JSONObject object = null;
                try {
                    object = new JSONObject(data);
                    int code = object.getInt("code");

                    LogUtils.w("code  == " + code + data);
                    Gson gson = new Gson();
                    mData = gson.fromJson(object.getString("data"), new TypeToken<List<Question_center>>() {
                    }.getType());
                    Message msg = new Message();
                    msg.what = code;
                    handler.sendMessage(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        setContentView(R.layout.fgm_base_refresh_layout);

        fgm_good_exchange_shop_btn_fabMenu = (FloatingActionsMenu) findViewById(R.id.fgm_good_exchange_shop_btn_fabMenu);
        fgm_good_exchange_shop_btn_fabMenu.setVisibility(View.GONE);
        super.onCreate(savedInstanceState);
    }


}
